﻿Imports Windows.Storage.Pickers
Imports Windows.Storage.Streams
Imports Windows.UI.Popups
Imports Windows.Storage

Namespace Utility
    Public Module UIHelper
        Public Async Function ShowFileDialog(extName As String, location As PickerLocationId) As Task(Of IRandomAccessStream)
            Dim picker As New FileOpenPicker
            picker.SuggestedStartLocation = location
            picker.FileTypeFilter.Add(extName)
            Dim file = Await picker.PickSingleFileAsync
            Return Await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
        End Function

        Public Async Function ShowFileDialog(extName As String) As Task(Of IRandomAccessStream)
            Return Await ShowFileDialog(extName, PickerLocationId.DocumentsLibrary)
        End Function

        ''' <summary>
        ''' 用来定义快捷菜单的大小及显示位置
        ''' </summary>
        ''' <param name="element"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetElementRect(element As FrameworkElement) As Rect
            Dim buttonTransform = element.TransformToVisual(Nothing)
            Dim point = buttonTransform.TransformPoint(New Point)
            Dim size As New Size(element.ActualWidth, element.ActualHeight)
            Return New Rect(point, size)
        End Function

        Public Async Sub ShowMessageDialog(msg As String, title As String, cmds As List(Of UICommand))
            Dim msgd As New MessageDialog(msg)
            msgd.Title = title
            For Each cmd In cmds
                msgd.Commands.Add(cmd)
            Next
            Await msgd.ShowAsync
        End Sub

        Public Async Function ShowPicSelecter(Optional location As PickerLocationId = PickerLocationId.PicturesLibrary) As Task(Of String)
            Dim picker As New FileOpenPicker
            picker.ViewMode = PickerViewMode.Thumbnail
            picker.SuggestedStartLocation = location
            picker.FileTypeFilter.Add(".jpg")
            picker.FileTypeFilter.Add(".jpeg")
            picker.FileTypeFilter.Add(".png")
            Dim file = Await picker.PickSingleFileAsync
            If file IsNot Nothing Then
                Return file.Path
            Else
                Return String.Empty
            End If
        End Function

        Public Async Function ShowVideoSelecter(Optional location As PickerLocationId = PickerLocationId.VideosLibrary) As Task(Of StorageFile)
            Dim picker As New FileOpenPicker
            picker.SuggestedStartLocation = location
            picker.FileTypeFilter.Add(".avi")
            picker.FileTypeFilter.Add(".mp4")
            Return Await picker.PickSingleFileAsync
        End Function

        Public Async Function ShowSaveFileDialog(extName As String, Optional location As PickerLocationId = PickerLocationId.VideosLibrary) As Task(Of StorageFile)
            Dim savePicker As New FileSavePicker
            savePicker.SuggestedStartLocation = location
            savePicker.DefaultFileExtension = extName
            savePicker.SuggestedFileName = "New File"
            savePicker.FileTypeChoices.Add(extName, {extName})
            Return Await savePicker.PickSaveFileAsync
        End Function
    End Module
End Namespace